package com.jt.controller;

import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.service.ItemCatService;
import com.jt.vo.SysResult;
import com.jt.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.jt.service.ItemService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@RequestMapping("/item")
@RestController
public class ItemController {

    @Autowired
    private ItemService itemService;

    @RequestMapping("/testVO")
    public EasyUITable testVO() {
        List<Item> rows = new ArrayList<>();
        rows.add(new Item().setPrice(10000l).setTitle("冰箱").setImage("bbbb"));
        return new EasyUITable(2000l, rows);
    }

    /*
        请求路径: http://localhost:8091/item/query?page=1&rows=20 get方法, 使用GetMapping
    */
    @GetMapping("/query")
    public EasyUITable doQuery(Integer page, Integer rows) {
        return itemService.findItemsByPage(page, rows);
    }


    @PostMapping("/save")
    public SysResult doSave(Item item, ItemDesc itemDesc) {
        int rows = itemService.saveItem(item, itemDesc);
        if (rows == 0) {
            return new SysResult(new RuntimeException("存储失败"));
        }
        return new SysResult();
    }

    @PostMapping("/update")
    public SysResult doUpdate(Item item, ItemDesc itemDesc) {
        int rows = itemService.updateItem(item, itemDesc);
        if (rows == 0) {
            throw new RuntimeException("更新失败");
        }
        return new SysResult("更新成功");
    }

    /*问题: 页面中的<input name="id" value="100">
     * 参数是如何接收的?
     * 本质上是底层调用request.getParameter("id")来获取数据.
     * 执行顺序:
     * String str = request.getParameter("id");
     * String[] idsStr = str.split(",");
     * Long[] idsLong = new Long[idsStr.length];
     * for(int i = 0; i < idsStr.length; i++){
     *     idsLong[i] = Long.parseLong(idsStr[i]);
     * }
     * return idsLong;
     * 因为上述的原因, 因此需要参数名称必须于页面中的属性一致
     * */
    @PostMapping("/delete")
    public SysResult doDelete(Long[] ids) {

        int rows = itemService.deleteByIds(ids);
        if (rows == 0) {
            throw new RuntimeException("删除失败");
        }
        return new SysResult("删除成功");
    }

//	@GetMapping("/param/query/itemcatid/{cid}")
//	public String doFindById(@PathVariable Long cid){
//		itemService.findById(cid);
//		return "ojbk";
//	}

//		RESTful风格的匹配关系:  优先匹配绝对路径, 然后在匹配可变路径,   匹配顺序根文件中的上下顺序有关

    /**
     * 该方法没有修改url路径, 修改时需要再次查询数据库
     *
     * @param upAndDown
     * @param ids
     * @return
     */
    @PostMapping("/{upAndDown}")
    public SysResult doUpAndDown(@PathVariable String upAndDown, Long[] ids) {
        if (!upAndDown.equals("instock") && !upAndDown.equals("reshelf")) {
            return SysResult.fault();
        }
        System.out.println(Arrays.toString(ids));
        int rows = itemService.changeStatus(ids);
        return new SysResult("ojbk");
    }


    /**
     * 该方法修改url路径, 将修改后的状态信息传入后台, 直接进行修改
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/updateStatus/{status}")
    public SysResult updateStatus(@PathVariable Integer status, Long[] ids) {
        int rows = itemService.updateStatus(status, ids);
        if (rows == 0) {
            return SysResult.fault();
        }
        return new SysResult("修改成功");
    }

    @GetMapping("/query/item/desc/{id}")
    public SysResult doDisplay(@PathVariable Long id) {
        ItemDesc itemDesc = itemService.findItemDescById(id);
        return new SysResult(itemDesc);
    }

}
